

package com.gc.server.service.impl;
import com.gc.server.dao.VoteDao;
import com.gc.server.entity.VoteEntity;
import com.gc.server.enums.BookStatusEnum;
import com.yf.response.RRException;
import org.springframework.transaction.annotation.Transactional;
import com.gc.server.vo.up.BookUp;
import org.springframework.beans.factory.annotation.Autowired;
import com.alibaba.excel.context.AnalysisContext;
import com.alibaba.excel.event.AnalysisEventListener;
import org.springframework.stereotype.Service;
import com.alibaba.excel.EasyExcel;
import com.alibaba.fastjson.JSON;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import  com.gc.server.vo.down.BookDown;

import java.io.InputStream;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.io.IOException;
import java.net.URLEncoder;
import com.gc.server.vo.down.BookDown;
import javax.servlet.http.HttpServletResponse;
import java.util.stream.Collectors;
import com.gc.server.dao.BookDao;
import com.gc.server.entity.BookEntity;
import com.gc.server.vo.BookVo;
import com.gc.server.service.BookService;
import com.yf.service.impl.BaseServiceImpl;

/**
 * 账户记录Service实现类
 *
 * @author hxf
 * @email 505860922@qq.com
 * @date Tue Oct 27 10:38:33 CST 2020
 */
@Service
public class BookServiceImpl extends BaseServiceImpl<BookEntity> implements BookService {

    private BookDao dao;


    @Autowired
    public void setDao(BookDao dao) {
        this.dao = dao;
        super.setBaseDao(dao);
    }




    @Override
    public List<BookVo> findVo(BookVo vo){
        return dao.findVo(vo);
    }

    @Override
    public BookVo getVo(Long id){
        return dao.getVo(id);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public int saveVo(BookVo vo){
        return dao.save(vo.getEntity());
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public int updateVo(BookVo vo){
        return dao.update(vo.getEntity());
    }



    @Override
    public void downLoad(HttpServletResponse response, List<BookVo> list) {
        try {
            List<BookDown> datas = list.stream().map(item -> new BookDown(item)).collect(Collectors.toList());
            response.setContentType("application/vnd.ms-excel" );
            response.setCharacterEncoding("utf-8" );
            // 这里URLEncoder.encode可以防止中文乱码 当然和easyexcel没有关系
            String fileName = URLEncoder.encode("测试", "UTF-8" );
            response.setHeader("Content-disposition", "attachment;filename=" + fileName + ".xlsx" );
            EasyExcel.write(response.getOutputStream(), BookDown.class).sheet("模板" ).doWrite(datas);
        } catch (Exception e) {
            e.printStackTrace();
        }

    }



    @Override
    public void upload(InputStream file) {
        EasyExcel.read(file, BookUp.class, new UploadDataListener()).sheet().doRead();
    }

    @Override
    public List<BookEntity> findByType(Integer type) {
        return dao.findByTypeAndStatus(type, BookStatusEnum.UP.getIndex());
    }

    @Autowired
    private VoteDao voteDao;

    @Override
    @Transactional
    public void vote(Long userId, Long bookId) {
        VoteEntity record = voteDao.findByUserIdAndBookId(userId,bookId);
        if(record!=null){
            throw new RRException("请勿重复投票!");
        }
        BookEntity bookEntity = dao.get(bookId);
        if(bookEntity==null){
            throw new RRException("绘本不存在");
        }
        dao.increaseVote();
        VoteEntity voteEntity = new VoteEntity(userId,bookId,new Date());
        voteDao.save(voteEntity);
    }

    class UploadDataListener extends AnalysisEventListener<BookUp> {

        @Override
        @Transactional(rollbackFor = Exception.class)
        public void invoke(BookUp upVo, AnalysisContext analysisContext) {
            saveVo(upVo.toVo());
        }

        @Override
        public void doAfterAllAnalysed(AnalysisContext analysisContext) {

        }

    }




    /**内部方法**/



    /**外部方法实现**/




}
